<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // ====实现链式调用
    // 方法一
    function Class1() {
      console.log('初始化');
    }
    Class1.prototype.method = function (params) {
      console.log(params)
      return this;
    }
    let c1 = new Class1();
    c1.method('第一次调用').method('第二次调用');

    // 方法二
    var obj = {
      a: function (a) {
        console.log(a);
        return this;
      },
      b: function (b) {
        console.log(b);
        return this;
      }
    }
    obj.a(1).b(2);

    // ====实现add(1)(2)(3)
    // function add(a) {
    //   return function (b) {
    //     return function (c) {
    //       return a + b + c
    //     }
    //   }
    // }
    // console.log(add(1)(2)(3));

    // const curry = (fn) =>
    //   (judge = (...args) =>
    //     args.length === fn.length ? fn(...args) : (...arg) => judge(...args, ...arg)
    //   )
    // const add  = (a,b,c) => a + b + c;
    // const curryAdd = curry(add);
    // console.log(curryAdd(1)(2)(3));


    function add(args) {
      return args.reduce((prev, next) => prev + next);
    }
    function currying (fn){
      let args = [];
      return function temp(...newArgs){
        console.log(newArgs);
        if(newArgs.length){
          args = [...args,...newArgs]
          return temp;
        }else{
          let val = fn(args);
          args = [];
          return val;
        }
      }
    }
    let addCurry = currying(add);
    console.log(addCurry(1)(2)(3,4)());



    // a.js
    var a = 1;
    function changeA(val){
      a = val;
    }
    module.exports = {
      a:a,
      changeA:changeA
    }
    // b.js
    var modA = require('./a.js');
    console.log('before',modA.a);  //输出1
    modA.changeA(2);
    console.log(modA.a);  //还是1
  </script>
</body>

</html>